package com.fone.player.view;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.fone.player.R;
import com.fone.player.client.HotspotRst.Cnt;
import com.fone.player.entity.RedirectInfo;
import com.fone.player.online.adapter.PostersAdapter;
import com.fone.player.play.PlayerFrom;
import com.fone.player.util.L;
import com.fone.player.util.ScreenUtil;

public class PostersView extends FrameLayout {

	private Context context;
	private ViewPager viewPager;
	private LinearLayout container;
//	private ViewPagerAdapter adapter;

	private List<RedirectInfo> posterList = new ArrayList<RedirectInfo>();
	// 底部小点的图片
	private ImageView[] points;

	// 记录当前选中位置
	private int currentIndex;
	private int nextIndex;
	private int pageNum = 1;

	private ImageView imageview;

	private PostersAdapter adapter;
	
	private static final int POSTER_MAX = 9;
	
	//private int mComeFrom = PlayerFrom.BANNER_COLLECTION;
	
	//private ScheduledExecutorService scheduledExecutorService;
	
	public PostersView(Context context) {
		this(context, null);
	}

	public PostersView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
		LayoutInflater.from(context).inflate(R.layout.view_posters, this, true);
		initView();
	}

//	public void initData(String shost, ArrayList<PosterInfo> posterList) {
//		//PM需求文档中 要求最多条数为8；
//		if(posterList.size() < POSTER_MAX){
//			this.posterList = posterList;
//			initPoint(posterList.size());
//		}else{
//			this.posterList = posterList.subList(0, 7);
//			initPoint(POSTER_MAX);
//		}
//		
//		
//		adapter = new PostersAdapter(getContext(), shost, posterList);
//		
//        viewPager.setAdapter(adapter);
//	}
	
	/** 
	 * 初始化海报
	 * 
	 * @param isHotSpot 是否来自"全网热点"
	 * @param shost 	服务器返回shost节点值
	 * @param cntList 	海报内容列表
	 *  
	 */
	public void initData(boolean isHotSpot, String shost, String host, List<?> cntList, int comefrom) {
		
		Log.i("PostersView", "initData");
		L.v("PostersView", "host == " + host);
		posterList = new ArrayList<RedirectInfo>();
		posterList.clear();
		
		if(isHotSpot){
			for (Object object : cntList) {
				Cnt cnt = (Cnt)object;
				RedirectInfo info = new RedirectInfo();
				info.utp = cnt.utp;
				info.weibourl = cnt.weibourl;
				info.url = cnt.url;
				info.vgurl = cnt.url;
				info.xyzplayurl = cnt.url;
				info.dnfs = cnt.dfnt;
				info.name = cnt.name;
				info.ourl = cnt.ourl;
				info.btnply = cnt.btnply;
				info.pic = cnt.pic2;
				info.host = host;
				
				posterList.add(info);
			}
		}else{
			
			for (Object object : cntList) {
				com.fone.player.client.SpecllistRst.Cnt cnt = (com.fone.player.client.SpecllistRst.Cnt)object;
				RedirectInfo info = new RedirectInfo();
				info.utp = cnt.utp;
				info.weibourl = cnt.weibourl;
				info.url = cnt.url;
				info.vgurl = cnt.url;
				info.xyzplayurl = cnt.url;
				info.dnfs = cnt.dfnt;
				info.name = cnt.name;
				info.ourl = cnt.ourl;
				info.btnply = cnt.btnply;
				info.pic = cnt.pic2;
				posterList.add(info);
			}
			
		}
		
		//PM需求文档中 要求最多条数为8；
		if(posterList.size() < POSTER_MAX){
			initPoint(posterList.size());
		}else{
			this.posterList = posterList.subList(0, 7);
			initPoint(POSTER_MAX);
		}
		
		adapter = new PostersAdapter(context, isHotSpot, shost, posterList, comefrom);
        viewPager.setAdapter(adapter);
        handler.removeMessages(0);
        handler.sendEmptyMessageDelayed(0, 5000);
  
        //setCurDot(0);
		//setCurView(0);
        
        
        //scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
        //每隔2秒钟切换一张图片
        //scheduledExecutorService.scheduleWithFixedDelay(new ViewPagerTask(), 2, 2, TimeUnit.SECONDS);
	}
	
	private void initView() {
		viewPager = (ViewPager) findViewById(R.posters.viewpager);
		viewPager.setOnPageChangeListener(new viewpagerListener());
		
	}
	
	/**
	 * 初始化底部小点
	 */
	private void initPoint(int num) {
		Log.i("PostersView", "initPoint");
		pageNum = num;
		
		container = (LinearLayout) findViewById(R.posters.container);
		container.removeAllViews();
		points = new ImageView[num];

		// 循环取得小点图片
		for (int i = 0; i < num; i++) {
			
			imageview = new ImageView(context);
			imageview.setBackgroundResource(R.drawable.home_banner_point_selector);
			
			LinearLayout.LayoutParams mLayoutParams = new LinearLayout.LayoutParams(ScreenUtil.dp2px(6), ScreenUtil.dp2px(6));  
			mLayoutParams.rightMargin = ScreenUtil.dp2px(4);
			mLayoutParams.leftMargin = ScreenUtil.dp2px(4);
			mLayoutParams.topMargin = ScreenUtil.dp2px(12);
			mLayoutParams.bottomMargin = ScreenUtil.dp2px(14);
			
			points[i] = imageview;
			
			// 设置位置tag，方便取出与当前位置对应
			points[i].setTag(i);
			// 默认都设为灰色
			points[i].setEnabled(true);
			// 给每个小点设置监听
			points[i].setOnClickListener(new pointListener());
			// 设置位置tag，方便取出与当前位置对应
			points[i].setTag(i);
			
			container.addView(points[i], mLayoutParams);
		}

		// 设置当面默认的位置
		currentIndex = 0;
		// 设置为白色，即选中状态
		points[currentIndex].setEnabled(false);
	}
	
	@Override
	protected void onDetachedFromWindow(){
		Log.i("PostersView", "onDetachedFromWindow");
		handler.removeMessages(0);
		super.onDetachedFromWindow();
	}
	
	private class viewpagerListener implements OnPageChangeListener {

		@Override
		public void onPageScrollStateChanged(int arg0) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onPageSelected(int position) {
			// TODO Auto-generated method stub
			// 设置底部小点选中状态
			setCurDot(position);
		}

	}
	
	private class pointListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			int position = (Integer) v.getTag();
			setCurView(position);
			setCurDot(position);
		}

	}

	/**
	 * 设置当前页面的位置
	 */
	private void setCurView(int position) {
		if (position < 0 || position >= pageNum) {
			return;
		}
		viewPager.setCurrentItem(position);
		
		Log.i("PostersView", "setCurView: " + position);
	}

	/**
	 * 设置当前的小点的位置
	 */
	private void setCurDot(int positon) {
		if (positon < 0 || positon > pageNum - 1 || currentIndex == positon) {
			return;
		}
		points[positon].setEnabled(false);
		points[currentIndex].setEnabled(true);

		currentIndex = positon;
	}
	
	/**
	 * 切换图片
	 */
//	private class ViewPagerTask implements Runnable {
//
//		@Override
//		public void run() {
//			nextIndex = (currentIndex + 1) % points.length;
//			// 更新界面
//			handler.obtainMessage().sendToTarget();
//		}
//
//	}

	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			if (points!= null && points.length >0) {
				nextIndex = (currentIndex + 1) % points.length;
				// 设置当前页面
				setCurDot(nextIndex);
				setCurView(nextIndex);
				handler.removeMessages(0);
				handler.sendEmptyMessageDelayed(0, 5000);
			}

		}
	};

}
